
#include "regulator.h"
#include <iomanip>
#include <memory.h>
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include "Atom.h"
#include "Translate.h"
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

// Result is assumed to be either 0 or 1
void Regulator::Monitor(int Result)
{
  /*outfile.setf(ios::left);
  outfile << setw(7) << NResult << setw(17) << *StepLength;
		switch(Result)
			{
			case 0:
				outfile << setw(15) << "Fail" << endl;
				break;
			default:
				outfile << setw(15) << "Pass" << endl;
				break;
			}*/


	NResult++; 	

  // Regulates the time-step so that there is a mProp percentage
  // of successes and failures (i.e. if mProp = 50% then there is
  // 50% of success and failures).
  double factor = mProp + double(NSuccess)/double(NResult);

  // NSuccess is non-zero
	if (NSuccess)
	{
    // !Result implies Result = 0
    if (!Result && factor >= 1.0)
    {
      // do nothing! I don't what to increase SL (StepLength=delta time) when fail
    }
    else if (Result && factor <= 1.0)
    {
      // do nothing! I don't what to decrease SL when success
    }
    else
    {
		  // based on trying to achieve some success rate
		
		  (*StepLength) *= factor;
    }
	}
	//cout << "Success rate " << double(NSuccess)/double(NResult) << endl;
}


void Regulator::PrintOpenNewFile(char name[])
{
	outfile.close();
	outfile.open(name);
}